package com.jzoom.zoom.admin.controllers;

import com.jzoom.zoom.admin.models.AdminDaoModel;
import com.jzoom.zoom.admin.models.ContentSercie;
import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.common.codec.Codec;
import com.jzoom.zoom.common.json.JSON;
import com.jzoom.zoom.dao.Dao;
import com.jzoom.zoom.dao.Page;
import com.jzoom.zoom.dao.Record;
import com.jzoom.zoom.web.annotations.Controller;
import com.jzoom.zoom.web.annotations.JsonResponse;
import com.jzoom.zoom.web.annotations.Mapping;
import com.jzoom.zoom.web.annotations.Param;
import com.jzoom.zoom.web.exception.StatusException;
import com.jzoom.zoom.web.exception.StatusException.NotFoundException;
import com.jzoom.zoom.web.view.View;
import com.jzoom.zoom.web.view.impl.StringView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区别于ContentController,本controller适配
 * {ds}/{module}/index|edit|add|del
 * 
 * 数据源+模块+方法
 * 
 * @author jzoom
 *
 */
@Controller(key = "")
public class DataSourceContentController  implements AdminController{

	@Inject
	private ContentSercie contentSercie;
	
	public DataSourceContentController() {
	
	}

	@Inject(value = "admin")
	private Dao admin;
	
 
	private AdminDaoModel getDao(String ds,String module) throws NotFoundException {
		return contentSercie.getDao(ds, module);
	}
	

	@Mapping(value = "{ds}/{module}/index", method = Mapping.GET)
	public StringView index(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module
			) throws NotFoundException {
		return createPage(ds,module, "/index");
	}

	/**
	 * 导出查询结果
	 *
	 * @return
	 */
	public View export(
			@Param(name = "{ds}") String ds,
			@Param(name = "{module}") String module,
			@Param(comment = "导出的base64条件")
					String condition) {
		Map<String, Object> search = JSON.parse(Codec.decodeBase64(condition), Map.class);
		return getDao(ds, module).export(search);
	}

	@Mapping(value = "{ds}/{module}/index", method = { Mapping.POST })
	@JsonResponse
	public Page<Record> index(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module,
			@Param(name="@")Map<String, Object> search) throws NotFoundException {
		return getDao(ds,module).getPage(search );
	}


	@Mapping(value = "{ds}/{module}/export", method = {Mapping.POST})
	public View export(@Param(name = "{ds}") String ds,
					   @Param(name = "{module}") String module, @Param(name = "{id}") String id,
					   @Param() String content
	) {
		Map<String, Object> search;
		try {
			search = JSON.parse(content, Map.class);
		} catch (Throwable t) {
			search = new HashMap<String, Object>();
		}

		return getDao(ds, module).export(search);
	}


	@Mapping(value = "{ds}/{module}/add", method = Mapping.GET)
	public StringView add(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module) throws NotFoundException {
		return createPage(ds,module, "/add");
	}
	

	@Mapping(value = "{ds}/{module}/edit", method = Mapping.GET)
	public StringView edit(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module) throws NotFoundException {
		return createPage(ds,module, "/edit");
	}



	
	
	@Mapping(value="{ds}/{module}/list",method= {Mapping.POST})
	@JsonResponse
	public List<Record> list(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module,
			@Param(name="@")Map<String, Object> params) throws NotFoundException{
		return getDao(ds,module).getList(  params );
	}
	
	
	@JsonResponse
	@Mapping(value = "{ds}/{module}/add", method = { Mapping.POST })
	public int add(
			@Param(name="{ds}")String ds,
			@Param(comment = "提交的表单信息", name = Param.BODY) Map<String, Object> data,
			@Param(name = "{module}") String module) throws NotFoundException {
		return getDao(ds,module).add(data);
	}

	@JsonResponse
	@Mapping(value = "{ds}/{module}/del/{id}", method = { Mapping.POST })
	public int del(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module,
			@Param(name = "{id}") Object id,
			@Param(body = true) Map<String, Object> data) throws NotFoundException {
		List<Object> ids = (List<Object>) data.get("_ids");
		if (ids != null && ids.size() > 0) {
			return getDao(ds, module).delMulti(ids);
		} else {
			return getDao(ds, module).del(id);
		}

	}

	@JsonResponse
	@Mapping(value = "{ds}/{module}/get/{id}", method = {Mapping.POST})
	public Record get(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module,
			@Param(name = "{id}") String id)
			throws NotFoundException {
		Record record = getDao(ds, module).get(id);
		if (record == null) {
			throw new StatusException.ApiError("id为" + id + "的数据已经删除，不能作为编辑的数据");
		}
		return record;
	}
	
	@JsonResponse
	@Mapping(value = "{ds}/{module}/put/{id}", method = { Mapping.POST })
	public int put(
			@Param(name="{ds}")String ds,
			@Param(name = "{module}") String module, @Param(name = "{id}") String id,
			@Param(name="@") Map<String, Object> data)
			throws NotFoundException {
		List<Object> ids = (List<Object>) data.get("_ids");
		data.remove("_ids");
		if (ids != null && ids.size() > 0) {
			return getDao(ds, module).putMulti(ids, data);
		} else {
			return getDao(ds, module).put(id, data);
		}

	}


	private StringView createPage(String ds,String module, String method) throws NotFoundException {
		return contentSercie.createPage(ds+"/"+module, method);
	}

}
